# -*- coding:utf-8 -*-
from wmcloud_dataclient import Client
from cassandra.cluster import Cluster
from cassandra import ConsistencyLevel
from cassandra.query import BatchStatement, BatchType
from datetime import *
import traceback
import json
import time
import os

def capture_wmcloud_foundation_raw_data(foundation_ticker_list = [], try_count = 3):
    client = Client('93ef5ebba60c5f1da190801750612423db818daad66155b6b191417b19d331b9')
    cluster = Cluster(protocol_version=4)
    session = cluster.connect()
    session.set_keyspace('wmcloud')
    insert_found = session.prepare('insert into wmcloud.foundation_daily_transanction_info (ticker, tradedate, circulationshares, exchangecd, secshortname, secid, chg, chgpct, closeprice, discount, discountratio, highestprice, lowestprice, openprice, precloseprice, turnovervalue, turnovervol) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
    end_datetime = datetime.today()
    weekend = int(end_datetime.strftime('%u')) - 5
    if weekend > 0:
        end_datetime = end_datetime - timedelta(days=weekend)
    end_time = str(end_datetime.strftime('%Y%m%d'))
    for foundation_ticker in foundation_ticker_list:
        row = session.execute("select tradedate from wmcloud.foundation_daily_transanction_info where ticker='%s' order by tradedate desc limit 1" % (foundation_ticker))
        row_l = list(row)
        if len(row_l) > 0:
            if row_l[0].tradedate.strftime('%Y%m%d') == end_time:
                # 说明已经下载了最新数据了不需要进一步的下载
                print('Skipped: %s' % (foundation_ticker))
                continue
            sdatet = row_l[0].tradedate + timedelta(days = 1)
            start_time = sdatet.strftime('%Y%m%d')
        else:
            start_time = '19970101'
        # 首先还是要通过cassandra的查询来获取某个基金的所保存的最近日期
        url = '/api/market/getMktFundd.json?field=&secID=&beginDate=%s&endDate=%s&ticker=%s' % (start_time, end_time, foundation_ticker)
        for try_c in range(try_count):
            try:
                code, result = client.getData(url)
                if code == 200:
                    foundation_json = json.loads(result)
                    print(foundation_ticker)
                    if 'retCode' not in foundation_json:
                        print('error json:', foundation_json)
                    elif foundation_json['retCode'] == 1:
                        batch_stmt = BatchStatement(batch_type=BatchType.UNLOGGED, consistency_level=ConsistencyLevel.QUORUM)
                        for inx, data in enumerate(foundation_json['data']):
                            #ticker, tradedate, circulationshares, exchangecd, secshortname, secid, chg, chgpct, closeprice, discount, discountratio, highestprice, lowestprice, openprice, precloseprice, turnovervalue, turnovervol
                            trade_date_timestamp = datetime.strptime(data['tradeDate'], '%Y-%m-%d') + timedelta(hours = 16)
                            batch_stmt.add(insert_found, (data['ticker'], trade_date_timestamp, data['circulationShares'] if 'circulationShares' in data else 0.0, data['exchangeCD'], data['secShortName'], data['secID'], data['CHG'] if 'CHG' in data else 0.0, data['CHGPct'] if 'CHGPct' in data else 0.0, data['closePrice'] if 'closePrice' in data else 0.0, data['discount'] if 'discount' in data else 0.0, data['discountRatio'] if 'discountRatio' in data else 0.0, data['highestPrice'] if 'highestPrice' in data else 0.0, data['lowestPrice'] if 'lowestPrice' in data else 0.0, data['openPrice'] if 'openPrice' in data else 0.0, data['preClosePrice'] if 'preClosePrice' in data else 0.0, data['turnoverValue'] if 'turnoverValue' in data else 0.0, data['turnoverVol'] if 'turnoverVol' in data else 0.0))
                            if inx != 0 and inx % 30 == 0:
                                session.execute(batch_stmt)
                                batch_stmt = BatchStatement(batch_type=BatchType.UNLOGGED, consistency_level=ConsistencyLevel.QUORUM)
                        else:
                            session.execute(batch_stmt)
                    break
            except Exception as e:
                print(e)
                continue

def get_foundation_ticker_list():
    cluster = Cluster(protocol_version=4)
    session = cluster.connect()
    session.set_keyspace('wmcloud')
    rows = session.execute('select ticker from wmcloud.foundation_basic_info')
    foundation_ticker_list = []
    for row in rows:
        foundation_ticker_list.append(row.ticker)

    session.shutdown()
    cluster.shutdown()
    return foundation_ticker_list

if __name__ == '__main__':
    try:
        foundation_ticker_list = get_foundation_ticker_list()
        capture_wmcloud_foundation_raw_data(foundation_ticker_list)
    except Exception as e:
        traceback.print_exc()
